<%@page import="weaver.conn.RecordSet"%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" import="com.zlgs.cqyf.vote.ConnectionUtil"%>
<%@ page import="java.io.PrintWriter" %>
<%@ page import="java.sql.ResultSet" %>
<%@ page import="weaver.hrm.User" %>
<%@ page import="weaver.hrm.HrmUserVarify" %>
<%@ page import="com.alibaba.fastjson.JSONObject" %>
<%
    response.setCharacterEncoding("UTF-8");
    response.setContentType("application/json; charset=utf-8");
    JSONObject retObject = new JSONObject();
    retObject.put("code", 200);
    retObject.put("msg", "操作成功");
    try {
        // 获取用户信息
        String sql="";
        String userId = "";
    	Object requestType = request.getParameter("requestType");
		if(null != requestType && !"".equals(requestType.toString())&&"mobile".equals(requestType.toString())){
			Object userIdObj = request.getParameter("userId");
			if(null == userIdObj || "".equals(userIdObj.toString())){
				throw  new RuntimeException("请求用户Id为空!");
			}
			userId = userIdObj.toString();
			sql = "select top 1    *  from HrmResource where   loginid = N'"+userId+"' ORDER BY ID DESC";
			RecordSet  set0 = new RecordSet();
		    set0.executeSql(sql);
		    if(!set0.next()){
		    	throw  new RuntimeException("请求用户信息异常!");
		    } 
			
			userId = userIdObj.toString();
		}else{
			if (!ConnectionUtil.is_local){
				User user = HrmUserVarify.getUser (request , response) ;
				if (null == user)  throw new RuntimeException("请重新登录");
				userId = user.getLoginid();
			}
			
		 }

        // URL 参数
        int awardsParticipatorId  = Integer.parseInt(request.getParameter("awardsParticipatorId"));  // 名单Id

        // 1.查询配置id
        sql = "SELECT a.awards_id FROM vote_awards_participator a  where a.id="+awardsParticipatorId;
        RecordSet set  = new RecordSet();
	  	set.executeSql(sql);
        int awardsId =0;
        while(set.next()){
            awardsId  = set.getInt("awards_id");
        }

        // 2.查询送花配置
        sql = "SELECT * FROM vote_awards  where id="+awardsId;
        RecordSet set2  = new RecordSet();
	  	set2.executeSql(sql);
        int flower = 0;
        while(set2.next()){
            flower  = set2.getInt("flower");
        }

        // 3.查询当前送花记录
        sql = "select * from vote_awards_flower where awards_id="+awardsId+" and user_id= N'"+userId+"'";
        RecordSet set3  = new RecordSet();
	  	set3.executeSql(sql);
        int allowance = flower-set3.getCounts(); // 剩余花
        boolean isFlower = false;           // 是否送过花
        while(set3.next()){
            if (awardsParticipatorId == set3.getInt("awards_participator_id")) isFlower = true;
        }
        // 4.插入数据(未送过，且有余量)
        if (!isFlower && allowance>0){
            sql = "INSERT INTO vote_awards_flower(awards_id,awards_participator_id,user_id,create_time) values ("+awardsId+","+awardsParticipatorId+",N'"+userId+"',GETDATE())";
            RecordSet set4  = new RecordSet();
    	  	set4.executeSql(sql);
            sql = "update vote_awards_participator set flower_num =(flower_num+1)  where id="+awardsParticipatorId;
            RecordSet set5  = new RecordSet();
    	  	set5.executeSql(sql);
        }
    } catch (Exception e) {
        e.printStackTrace();
        retObject.put("code", -1);
        retObject.put("msg", e.getMessage());
    } finally {
        PrintWriter printWriter =   response.getWriter();
        printWriter.append(retObject.toJSONString());
        if (printWriter != null) printWriter.close();
    }
%>
